﻿using System;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using ComToolkit.IO;

namespace ComToolkit.Demo.Views
{
    public partial class IO : Page
    {
        public IO()
        {
            InitializeComponent();
            DirectoryPath.Text = @"C:\ComToolkit";
            FilePath.Text = @"C:\ComToolkit\testfile.bin";
        }

        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
        }

        private void ButtonCreateDirectory_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (ComToolkit.IO.Directory.Exists(DirectoryPath.Text))
                {
                    MessageBox.Show(string.Format("Directory: {0} allready exist.", DirectoryPath.Text));
                }
                else
                {
                    ComToolkit.IO.Directory.CreateDirectory(DirectoryPath.Text);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void ButtonDeleteDirectory_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (ComToolkit.IO.Directory.Exists(DirectoryPath.Text))
                {
                    if (MessageBox.Show(
                        string.Format("Are you sure you want to delete directory: {0}", DirectoryPath.Text),
                        "Confirm",
                        MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                    {
                        ComToolkit.IO.Directory.Delete(DirectoryPath.Text);
                    }
                }
                else
                {
                    MessageBox.Show(string.Format("Directory: {0} does not exist.", DirectoryPath.Text));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void ButtonCreateFile_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (ComToolkit.IO.File.Exists(FilePath.Text))
                {
                    MessageBox.Show(string.Format("File: {0} allready exist.", FilePath.Text));
                }
                else
                {
                    ComToolkit.IO.File.Create(FilePath.Text);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void ButtonDeleteFile_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (ComToolkit.IO.File.Exists(FilePath.Text))
                {
                    if (MessageBox.Show(
                        string.Format("Are you sure you want to delete the file: {0}", FilePath.Text),
                        "Confirm",
                        MessageBoxButton.OKCancel) == MessageBoxResult.OK)
                    {
                        ComToolkit.IO.File.Delete(FilePath.Text);
                    }
                }
                else
                {
                    MessageBox.Show(string.Format("File: {0} does not exist.", FilePath.Text));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void ButtonOpenFile_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (ComToolkit.IO.File.Exists(FilePath.Text))
                {
                    TextEditor.Text = ComToolkit.IO.File.ReadAllText(FilePath.Text);
                }
                else
                {
                    MessageBox.Show(string.Format("File: {0} does not exist, please create it first.", FilePath.Text));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void ButtonSave_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                ComToolkit.IO.File.WriteAllText(FilePath.Text, TextEditor.Text);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

        private void ButtonListDirectory_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                if (ComToolkit.IO.Directory.Exists(DirectoryPath.Text))
                {
                    StringBuilder sb = new StringBuilder();
                    Directory.EnumerateDirectories(DirectoryPath.Text).ForEach(s => sb.AppendLine(s));
                    sb.AppendLine();
                    Directory.EnumerateFiles(DirectoryPath.Text).ForEach(s => sb.AppendLine(s));

                    TextEditor.Text = sb.ToString();
                }
                else
                {
                    MessageBox.Show(string.Format("Directory: {0} does not exist.", DirectoryPath.Text));
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

    }
}
